package cc.ufinity.networkdiagnose.widget;

import java.util.List;
import java.util.Map;

import cc.ufinity.networkdiagnose.manager.MessageWhat;
import cc.ufinity.networkdiagnose.manager.TelManager;
import cc.ufinity.networkdiagnose.manager.WlanManager;
import cc.ufinity.networkdiagnose.view.R;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class WifiSnrTestWidget extends GeneralTestWidget {
	private WlanManager wlanManager;
	private ListView wifiView;
	private Activity ctx;
	private Button btnBack;
	private Handler handler;
	private TelManager telManager;

	public WifiSnrTestWidget(Activity context) {
		super(context);
		this.ctx = context;
		inflate(context, R.layout.wifi_snr_test_widget, this);
		btnBack = (Button) this.findViewById(R.id.wifisnr_test_btnback);
		wifiView = (ListView) this.findViewById(R.id.wifiSnrListView);
		btnBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				back(ctx);
			}
		});
		handler = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MessageWhat.WIFI_RECEIVE_LIST:
					updateWifi();
					dismissMessageDialog();
					break;
				}
			}
		};

		wlanManager = new WlanManager(ctx);
		telManager = new TelManager(ctx);
		telManager.setHandler(handler);
		wlanManager.setHandler(handler);
		wlanManager.setCdmaDbm(telManager.getCdmaDbM());
		start();
	}

	private void start() {
		this.showMessageDialog(ctx, "正在进行信噪比测试...");
		wlanManager.startScan();
	}

	private void updateWifi() {
		List<Map<String, Object>> data = wlanManager.getWifiList();
		SimpleAdapter adapter = new SimpleAdapter(ctx, data,
				R.layout.wifi_snr_widget,
				new String[] { "ssid", "mac", "snr" }, new int[] {
						R.id.wifi_ssid, R.id.wifi_mac, R.id.wifi_snr });
		wifiView.setAdapter(adapter);
	}

}
